/*
 * @Description:
 * @Author: Rfan
 * @Date: 2022-01-25 17:52:46
 * @LastEditTime: 2022-03-21 10:48:14
 */

import { Tabs } from 'antd';
import { useAccessionContext } from '../contexts/AccessionContext';
import LevelTable from './LevelTable';
import FileLevelTable from './FileLevelTableWrap';

const { TabPane } = Tabs;

const LevelWrap = () => {
  const { levelList, tabList, currentLevel, setCurrentData } = useAccessionContext();

  /**
   * @description: 计算所有可被渲染的tab
   */
  const allTabList = () => {
    if (levelList.length < 3) {
      return levelList.slice(0, -1);
    }
    return levelList.slice(0, -2);
  };

  const handleChangeTab = (val: any) => {
    console.log('change', val);
    setCurrentData(val);
  };

  return (
    <Tabs activeKey={currentLevel} onChange={handleChangeTab}>
      {tabList.map((item: any, idx: number) => {
        console.log('render tabs', idx, item, tabList.length - 1);

        return idx === allTabList().length - 1 ? (
          <TabPane tab={item.levelName} key={item.level}>
            <FileLevelTable data={item} />
          </TabPane>
        ) : (
          <TabPane tab={item.levelName} key={item.level}>
            <LevelTable data={item} />
          </TabPane>
        );
      })}
    </Tabs>
  );
};

export default LevelWrap;
